﻿using System;
using System.Collections.Generic;
using System.Text;
using Fsb.Utility;
using VideoCache.Aggregation.Interfaces;

namespace VideoCache.Aggregation.Implements
{
    public class BaiduAlbumSearcher : IAlbumSearcher
    {
        public List<Album> Search(string keyword)
        {
            string url = string.Format("http://video.baidu.com/v?word={0}", System.Web.HttpUtility.UrlEncode(keyword, Encoding.GetEncoding("gb2312")));

            string html = HttpUtility.Get(url, Encoding.GetEncoding("gb2312"));

            string scriptContent = StringUtility.SubString(html, "globalVars.aldJson = [];", "var isResult").Trim();

            List<Album> albumList = new List<Album>();

            if (scriptContent.Length > 0)
            {
                string[] list = scriptContent.Split(new string[] { "globalVars.aldJson.push(T.object.extend(" }, StringSplitOptions.RemoveEmptyEntries);

                for (int i = 1; i < list.Length; i++)
                {
                    string itemJson = StringUtility.SubString(list[i], "", ", {\"alias\"");
                    if (!string.IsNullOrEmpty(itemJson))
                    {
                        albumList.Add(this.ParseFromJson(itemJson));
                    }
                }
            }

            return albumList;
        }

        private Album ParseFromJson(string json)
        {
            Newtonsoft.Json.Linq.JObject obj = Newtonsoft.Json.Linq.JObject.Parse(json);

            Album album = new Album();

            album.Title = obj["title"].ToString().Trim('"');

            int type;
            if (int.TryParse(obj["ty"].ToString().Trim('"'), out type))
            {
                switch (type)
                {
                    case 21:
                        album.Type = AlbumType.Movie;
                        break;
                    case 22:
                        album.Type = AlbumType.Tv;
                        break;
                }
            }

            int year;

            switch (album.Type)
            {
                case AlbumType.Tv:
                    int.TryParse(obj["al_date"].ToString().Trim('"'), out year);
                    break;
                case AlbumType.Movie:
                    int.TryParse(obj["pubtime"].ToString().Trim('"'), out year);
                    break;
                default:
                    throw new NotImplementedException();
            }

            album.Year = year;

            foreach (var director in obj["director"])
            {
                album.Directors.Add(director.ToString().Trim('"'));
            }

            foreach (var actor in obj["actor"])
            {
                album.Actors.Add(actor.ToString().Trim('"'));
            }

            album.Brief = obj["brief"].ToString().Trim('"');

            foreach (var item in obj["type"])
            {
                album.Tags.Add(item.ToString().Trim('"'));
            }

            foreach (var item in obj["area"])
            {
                album.Tags.Add(item.ToString().Trim('"'));
            }

            foreach (var item in obj["alias"])
            {
                string tag = item.ToString().Trim('"');

                album.Tags.Add(tag);

                if (!string.IsNullOrEmpty(album.SubTitle))
                {
                    album.SubTitle += ",";
                }

                album.SubTitle += tag;
            }

            return album;
        }
    }
}